package com.engc.common.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.engc.common.service.BaseService;
import com.engc.common.utils.BaseUtils;

@Controller
@RequestMapping("/systemMenu")
public class SystemMenu {
	private BaseService baseService;

	@RequestMapping("/getMenus")
	@ResponseBody
	public List<Map<String, Object>> getMenu(HttpServletRequest request) {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		//新生认证管理
		Map<String,Object> menuMap = new HashMap<String, Object>();
		menuMap.put("menuname", "新生认证管理");
		
		Map<String,Object> subMenu = new HashMap<String, Object>();
		subMenu.put("menuname", "2.个人信息认证");
		subMenu.put("menuid", "3");
		subMenu.put("menuurl", "person/view");
		subMenu.put("icon", "resource/icons/32X32/logout.gif");
		
		Map<String,Object> subMenu10 = new HashMap<String, Object>();
		subMenu10.put("menuname", "教师管理");
		subMenu10.put("menuid", "10");
		subMenu10.put("menuurl", "user/view");
		subMenu10.put("icon", "resource/icons/32X32/logout.gif");
		
		Map<String,Object> subMenu2 = new HashMap<String, Object>();
		subMenu2.put("menuname", "1.电子档案登记");
		subMenu2.put("menuid", "2");
		subMenu2.put("menuurl", "stu/view");
		subMenu2.put("icon", "resource/icons/32X32/logout.gif");
		
		Map<String,Object> subMenu11 = new HashMap<String, Object>();
		subMenu11.put("menuname", "1.新生信息导入");
		subMenu11.put("menuid", "1");
		subMenu11.put("menuurl", "stu/import");
		subMenu11.put("icon", "resource/icons/32X32/logout.gif");
		
		Map<String,Object> subMenu3 = new HashMap<String, Object>();
		subMenu3.put("menuname", "3.个人资料收取");
		subMenu3.put("menuid", "4");
		subMenu3.put("menuurl", "receive/view");
		subMenu3.put("icon", "resource/icons/32X32/logout.gif");
		
		List<Map<String, Object>> temp = new ArrayList<Map<String, Object>>();
		temp.add(subMenu11);
		temp.add(subMenu2);
		temp.add(subMenu);
		temp.add(subMenu3);
		temp.add(subMenu10);
		menuMap.put("children", temp);
		
		list.add(menuMap);
		
		//2.缴费注册管理
		Map<String,Object> menuMap2 = new HashMap<String, Object>();
		menuMap2.put("menuname", "缴费注册管理");
		
		Map<String,Object> subMenu00 = new HashMap<String, Object>();
		subMenu00.put("menuname", "缴费管理");
		subMenu00.put("menuid", "4");
		subMenu00.put("menuurl", "nopay/view");
		subMenu00.put("icon", "resource/icons/32X32/logout.gif");
//		
//		Map<String,Object> subMenu22 = new HashMap<String, Object>();
//		subMenu22.put("menuname", "已缴费新生");
//		subMenu22.put("menuid", "2");
//		subMenu22.put("menuurl", "pay/view");
//		subMenu22.put("icon", "resource/icons/32X32/logout.gif");
		
		List<Map<String, Object>> temp2 = new ArrayList<Map<String, Object>>();
		temp2.add(subMenu00);
//		temp2.add(subMenu22);
		menuMap2.put("children", temp2);
		
		list.add(menuMap2);
		
		//2.班级事务管理
		Map<String,Object> menuMap3 = new HashMap<String, Object>();
		menuMap3.put("menuname", "班级事务管理");
		
		Map<String,Object> subMenu000 = new HashMap<String, Object>();
		subMenu000.put("menuname", "学习事务管理");
		subMenu000.put("menuid", "5");
		subMenu000.put("menuurl", "study/view");
		subMenu000.put("icon", "resource/icons/32X32/logout.gif");
		
		Map<String,Object> subMenu222 = new HashMap<String, Object>();
		subMenu222.put("menuname", "生活事务管理");
		subMenu222.put("menuid", "6");
		subMenu222.put("menuurl", "life/view");
		subMenu222.put("icon", "resource/icons/32X32/logout.gif");
		
		List<Map<String, Object>> temp3 = new ArrayList<Map<String, Object>>();
		temp3.add(subMenu000);
		temp3.add(subMenu222);
		menuMap3.put("children", temp3);
		
		list.add(menuMap3);
		
		//2.军训事务管理
		Map<String,Object> menuMap4 = new HashMap<String, Object>();
		menuMap4.put("menuname", "军训事务管理");
		
		Map<String,Object> subMenu0000 = new HashMap<String, Object>();
		subMenu0000.put("menuname", "军训物资分配");
		subMenu0000.put("menuid", "7");
		subMenu0000.put("menuurl", "armyGoods/view");
		subMenu0000.put("icon", "resource/icons/32X32/logout.gif");
		
		Map<String,Object> subMenu2222 = new HashMap<String, Object>();
		subMenu2222.put("menuname", "军训情况登记");
		subMenu2222.put("menuid", "8");
		subMenu2222.put("menuurl", "armySit/view");
		subMenu2222.put("icon", "resource/icons/32X32/logout.gif");
		
		List<Map<String, Object>> temp4 = new ArrayList<Map<String, Object>>();
		temp4.add(subMenu0000);
		temp4.add(subMenu2222);
		menuMap4.put("children", temp4);
		
		list.add(menuMap4);
		return list;
	}

	@RequestMapping("/view")
	public String goView() {
		return "system/sysMenu/menu";
	}

	@RequestMapping("updateMenu")
	@ResponseBody
	public Map<String, Object> updateMenu(HttpServletRequest request) {
		Map<String, Object> record = new HashMap<String, Object>();
		record.put("menuorder", request.getParameter("menuorder"));
		record.put("menuname", request.getParameter("menuname"));
		record.put("menuno", request.getParameter("menuno"));
		record.put("menuurl", request.getParameter("menuurl"));
		record.put("menuicon", request.getParameter("menuicon"));
		record.put("isvisible", 1);
		record.put("isleaf", 1);
		try {
			baseService.Update("sys_menu", record, " menuid=? ", request.getParameter("menuid"));
			return BaseUtils.getSuccessMessage("更新成功");
		} catch (Exception e) {
			e.printStackTrace();
			return BaseUtils.getErrorMessage("更新失败");
		}
	}

	@RequestMapping("addMenu")
	@ResponseBody
	public Map<String, Object> insertMenu(HttpServletRequest request) {
		Map<String, Object> record = BaseUtils.getInsertParam(request);
		record.put("isVisible", 1);
		record.put("isLeaf", 1);
		record.put("menuid", baseService.getSeqValue("seq_sys_menu"));
		try {
			baseService.Insert("sys_menu", record);
			return BaseUtils.getSuccessMessage("添加成功");
		} catch (Exception e) {
			e.printStackTrace();
			return BaseUtils.getErrorMessage("添加失败");
		}
	}

	@RequestMapping("/delMenu")
	@ResponseBody
	public Map<String, Object> delMenu(HttpServletRequest request) {
		String menuId = request.getParameter("menuid");
		try {
			baseService.Delete("sys_menu", " menuid=? ", menuId);
			return BaseUtils.getSuccessMessage("删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			return BaseUtils.getSuccessMessage("删除失败");
		}
	}

	@RequestMapping("/addButton")
	@ResponseBody
	public Map<String, Object> addButton(HttpServletRequest request) throws Exception {
		Map<String, Object> record = BaseUtils.getInsertParam(request);
		record.put("seqno", 0);
		record.put("btnid", baseService.getSeqValue("seq_sys_button"));
		baseService.Insert("sys_button", record);
		return BaseUtils.getSuccessMessage("添加按钮成功");
	}

	@RequestMapping("/updateButton")
	@ResponseBody
	public Map<String, Object> updateButton(HttpServletRequest request) throws Exception {
		Map<String, Object> record = BaseUtils.getUpdateParam(request, "btnID");
		baseService.Update("sys_button", record, "btnid=?", request.getParameter("btnID"));
		return BaseUtils.getSuccessMessage("更新按钮成功");
	}

	@RequestMapping("/delButton")
	@ResponseBody
	public Map<String, Object> delButton(HttpServletRequest request) throws Exception {
		String ButtonId = request.getParameter("btnID");
		try {
			baseService.Delete("sys_button", "btnid=?", ButtonId);
			return BaseUtils.getSuccessMessage("删除按钮成功");
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}

	@RequestMapping("/clearButton")
	@ResponseBody
	public Map<String, Object> clearButton(HttpServletRequest request) {
		String menuNo = request.getParameter("menuNo");
		try {
			baseService.Delete("sys_button", "menuno=?", menuNo);
			return BaseUtils.getSuccessMessage("清空按钮成功");
		} catch (Exception e) {
			e.printStackTrace();
			return BaseUtils.getErrorMessage("清空按钮成功");
		}
	}

	@RequestMapping("/addCUDButtons")
	@ResponseBody
	public Map<String, Object> addCUDButtons(HttpServletRequest request) throws Exception {
		String menuno = request.getParameter("menuNo");
		Map<String, Object> record = new HashMap<String, Object>();
		record.put("btnno", "add");
		record.put("btnName", "新增");
		record.put("btnicon", "resource/icons/silkicons/add.png");
		record.put("menuno", menuno);
		record.put("btnid", baseService.getSeqValue("seq_sys_button"));
		baseService.Insert("sys_button", record);
		record = new HashMap<String, Object>();
		record.put("btnno", "modify");
		record.put("btnName", "修改");
		record.put("btnicon", "resource/icons/silkicons/application_edit.png");
		record.put("menuno", menuno);
		record.put("btnid", baseService.getSeqValue("seq_sys_button"));
		baseService.Insert("sys_button", record);
		record = new HashMap<String, Object>();
		record.put("btnno", "delete");
		record.put("btnName", "删除");
		record.put("btnicon", "resource/icons/silkicons/delete.png");
		record.put("menuno", menuno);
		record.put("btnid", baseService.getSeqValue("seq_sys_button"));
		baseService.Insert("sys_button", record);
		record = new HashMap<String, Object>();
		record.put("btnno", "view");
		record.put("btnName", "查看");
		record.put("btnicon", "resource/icons/silkicons/application_view_detail.png");
		record.put("menuno", menuno);
		record.put("btnid", baseService.getSeqValue("seq_sys_button"));
		baseService.Insert("sys_button", record);
		return BaseUtils.getSuccessMessage("批量添加按钮成功");
	}

	@Resource(name = "baseService")
	public void setBaseService(BaseService baseService) {
		this.baseService = baseService;
	}

}
